import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.List;

public class SearchFragment extends Fragment {

    private EditText searchEditText;
    private ListView productListView;
    private List<String> allProducts;
    private ProductAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.search_fragment, container, false);

        searchEditText = view.findViewById(R.id.search_edit_text);
        productListView = view.findViewById(R.id.product_list_view);

        // 模拟一些农产品数据
        allProducts = new ArrayList<>();
        allProducts.add("苹果");
        allProducts.add("香蕉");
        allProducts.add("橙子");
        allProducts.add("西瓜");
        allProducts.add("葡萄");

        adapter = new ProductAdapter(getContext(), allProducts);
        productListView.setAdapter(adapter);

        searchEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                filterProducts(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        return view;
    }

    private void filterProducts(String keyword) {
        List<String> filteredProducts = new ArrayList<>();
        for (String product : allProducts) {
            if (product.contains(keyword)) {
                filteredProducts.add(product);
            }
        }
        adapter.updateData(filteredProducts);
    }

    class ProductAdapter extends BaseAdapter {

        private List<String> productList;
        private Context context;

        public ProductAdapter(Context context, List<String> productList) {
            this.context = context;
            this.productList = productList;
        }

        public void updateData(List<String> newData) {
            productList = newData;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return productList.size();
        }

        @Override
        public Object getItem(int i) {
            return productList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ListView listView) {
            if (view == null) {
                view = View.inflate(context, R.layout.product_item, null);
            }
            // 设置产品名称显示等
            return view;
        }
    }
}